Getting All Keyed Up I

Finally, some keyboard input! Type a letter or two on the keyboard; the script will capture it and echo it into the form below.
Key pressed: with keyboard modifiers

Discussion

You're just going to have to trust us here: neither of the form text fields has input focus while you're typing! We deliberately put quotes around the incoming characters to persuade you that the event handler is getting the keys, not the input text field. A quick peek at the code for this script will prove we're on the up-and-up!

One thing you can't do easily is capture the state of the Ctrl and Alt keys while handling keyboard input. This is because the browser program itself expects to catch key sequences like Ctrl-A (mark all text) or Alt-E (open Edit menu), and gets them before the JavaScript engine does. Catching Shift seems to work every time.

The script as written will try to echo non-printable characters into the form (press Enter or Backspace to see this). As an exercise, modify the event handler to echo only printable characters (character codes 32 through 127) into the form.

Copyright ©1998 by Charles River Media, All Rights Reserved